import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import type { SocialAccount } from '@/types'

export const useAccountsStore = defineStore('accounts', () => {
  // State
  const accounts = ref<SocialAccount[]>([])
  const loading = ref(false)
  const error = ref<string | null>(null)

  // Getters
  const getAccountsByPlatform = computed(() => {
    return (platform: string) => accounts.value.filter(account => account.platform === platform)
  })

  const getActiveAccounts = computed(() => {
    return accounts.value.filter(account => account.status === 'active')
  })

  const totalAccounts = computed(() => accounts.value.length)

  // Actions
  const fetchAccounts = async() => {
    loading.value = true
    error.value = null
    try {
      // TODO: 实现获取账号列表的逻辑
      console.log('Fetching accounts...')
    } catch (err) {
      error.value = err instanceof Error ? err.message : 'Unknown error'
    } finally {
      loading.value = false
    }
  }

  const addAccount = async(account: Omit<SocialAccount, 'id' | 'created_at' | 'updated_at'>) => {
    // TODO: 实现添加账号的逻辑
    console.log('Adding account:', account)
  }

  const updateAccount = async(id: string, updates: Partial<SocialAccount>) => {
    // TODO: 实现更新账号的逻辑
    console.log('Updating account:', id, updates)
  }

  const removeAccount = async(id: string) => {
    // TODO: 实现删除账号的逻辑
    console.log('Removing account:', id)
  }

  return {
    // State
    accounts,
    loading,
    error,

    // Getters
    getAccountsByPlatform,
    getActiveAccounts,
    totalAccounts,

    // Actions
    fetchAccounts,
    addAccount,
    updateAccount,
    removeAccount
  }
})